package common.liusany.admin.web.admin;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liusany.modules.orm.Page;
import com.liusany.modules.orm.PropertyFilter;
import common.liusany.admin.entity.account.Group;
import common.liusany.admin.entity.account.Permission;
import common.liusany.admin.service.account.AccountManager;

@Controller
@RequestMapping(value = "/admin/group")
public class GroupController extends BaseCopntroller {

	private AccountManager accountManager;
	

	@RequestMapping(value = { "list", "" })
	public String list(Model model, Page pager ) {
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		pager = accountManager.findGroupByPage(pager,filters);
		model.addAttribute("pager", pager);
		return "admin/groupList";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("group", new Group());
		model.addAttribute("allPermissions", Permission.values());
		return "admin/groupForm";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Group group,Model model) {
		accountManager.saveGroup(group);
		setRedirectInfo("admin/group/list", "创建权限组" + group.getName() + "成功", model);
		return SUCCESS;
	}

	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(Long[] ids) {
		accountManager.deleteGroup(ids);
		
		//在使用@ResponseBody注解返回json格式的数据时直接返回String类型会出现乱码 
		//改为其他类型不会例如map list...
		return ajaxJsonSuccessMessage("删除权限组成功");
	}

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

}
